$(document).ready(
				function() {
					
					//富文本编辑器
					var ue=UE.getEditor('color');
					
					//获取项目名
					var contextPath = document.location.pathname; 
					var index =contextPath.substr(1).indexOf("/"); 
					contextPath = contextPath.substr(0,index+1); 
					
					var totalCount = $("#totalCount").val();
					var pSize=$("#pageSize").val();;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					
					var promtArry = new Array()// 定义一个数组，放提示信息
					var promtNum = 0;

					$("#gdDetails").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#giftDetail").attr("method","post");
										
										var gdId = $("#gdId").val();
									
											if (gdId =="") {
												$("#giftDetail").attr("action","save.do");
											} else {
												$("#giftDetail").attr("action","update.do");
											}
											$("#giftDetail").submit();
										
									});

					$('#pagination2').jqPaginator(
									{
										/*totalPages: pageCount,
									    visiblePages: 10,
									    currentPage: 1,*/
										totalCounts:totalCount,
										pageSize: pSize,
									    visiblePages: pageCount,
									    currentPage: 1,
									
									    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
									    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
									    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											$("#pageIndex").val(num);
											var giftInfosId=$("#giftInfosId").val();
//											alert("sdhgsjf===========>"+giftInfosId);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize,
															id:giftInfosId
														},
														cache : false,
														success : function(data) {
															$("#gdList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var gd = results[i];
																var rowContent = "<tr>"
																	+ "<td><input name='gdId' id='gdId' class='checkbox' type='checkbox' value='"
																	+ gd.id
																	+ "'/></td>"
																	+ "<td>"
																	+ gd.giftCode
																	+ "</td>"
																	+ "<td>"
																	+ gd.giftName
																	+ "</td>"
																	
																	+ "<td><div style=' width:100px; height:30px;overflow:hidden;'>" 
																	+ "<a href = 'javascript:void(0)' onclick = 'aa();'>" 
																	+ "<img src='"+contextPath+"/HRTWEB/images/23/" 
																	+gd.pic 
																	+ "'border='0' width='100px' />"
																	+ "</a></div>" 
																	+ "<div id='light' class='white_content'>  " 
																	+ "<a href = 'javascript:void(0)' onclick = 'bb();'> " 
																	+ "<img src='"+contextPath+"/HRTWEB/images/23/" 
																	+ gd.pic 
																	+ "' /> </a> </div>  " 
																	+ "<div id='fade' class='black_overlay'></div>" 
																	+ "</td>"
																	
																	/*+ "<td>"
																	+ "<button id='addColor' class='btn btn-primary btn-large' type='button' data-toggle='modal' >添加颜色</button>"
																	+ "</td>"
																	*/
																	+ "<td>"
																	+ gd.salePrice
																	+ "</td>"
																	+ "<td> 长：" 
																	+ gd.longiness 
																	+ "宽：" 
																	+ gd.width
																	+ "高：" 
																	+ gd.height
																	+ "</td>"
																	
																	+ "<td>"
																	if (gd.ispush == 0) {
																		rowContent = rowContent
																				+ "下架";
																	} else {
																		rowContent = rowContent
																		+ "<font color='red'>上架</font>";
																	}
																	+ "</td>"
																rowContent = rowContent
																		+ "<td>" 
																		+ "<button id='viewGroup' objId='"
																		+ gd.id 
																		+ "' class='btn btn-sm-block'>组合管理</button>" 
																		+ "</td>"
																		+ "<td>" 
																		+ "<button id='editBtn' objId='"
																		+ gd.id
																		+ "' class='btn btn-sm-block'>修改</button>" 
																		+ "</td>" 
																		+ "<td>"
																		+ "<button id='checkBtn' name='checkBtn' objId='"
																		+ gd.id
																		+ "' class='btn btn-sm-block'>查看</button></td>"
																		+ "</tr>";
																$("#gdList")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															alert("bbbbbbbbbbb"+html);
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#gdList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='gdId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					
					function clearItemTable(selectItem) {

						$("#"+selectItem+" tr:not(:first)").remove();
						rowNum = 0;
					}
					
					/**
					 * 修改按钮事件
					 */

					$("#gdList").delegate('#editBtn', 'click', function() {
						$("#checks input").removeAttr("readonly");
						$("#checks textarea").removeAttr("readonly");
						$("#checks select").removeAttr("disabled");
						$(".readonlyss").attr({ readonly: 'true' });
						//初始化表单的验证提示信息
						var i = 0;
						$(".jqtransform").Validform().resetForm();
						$(".Validform_checktip").each(function() {
							if(promtArry.length > 0 && promtArry != null){
								$(this).text(promtArry[i]);
								$(this).removeAttr("class");  
								$(this).attr("class","Validform_checktip");
								$(this).parent().prev().children().removeAttr("class");
								$(this).parent().prev().children().attr("class","inputxt Validform_error");
							}
							i++;
						});
						
						var gdId = $(this).attr("objId");
						
						var editUrl = "show.do?gdId=" + gdId;
						$.ajax({

							url : editUrl,
							dataType : 'json',                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
							data : {
								id : gdId
							},
							cache : false,

							success : function(data) {
								
								loadGI(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});

					});
					
					/**
					 * 查看详情
					 */
					$("#gdList").delegate('#checkBtn', 'click', function() {
						var gdId = $(this).attr("objId");
						
						var editUrl = "show.do?gdId=" + gdId;
						$.ajax({

							url : editUrl,
							dataType : 'json',                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
							data : {
								id : gdId
							},
							cache : false,

							success : function(data) {
								
								loadGI(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});
						$("#checks input").attr({ readonly: 'true' });
						$("#checks textarea").attr({ readonly: 'true' });
						$("#checks select").attr("disabled","disabled");
					});

					
					/**
					  删除按钮事件
					 */
					$("#tools").delegate(
						'#deleteBtn',
						'click',
						function() {
							delChk = $('input[name="gdId"]:checked');

							if (delChk.length > 0) {
								if (confirm("Are you sure delete the record?")) {

									var ids = "";
									var delUrl = "delete.do";
                                    var idArr=new Array();
                                   
                                    
                                    for (var i = 0; i < delChk.length; i++) {
                                    	idArr.push(delChk.get(i).value);
                               
								}
									
									$.ajax({
												url : delUrl,
												type: "POST",
												data : {
													ids : idArr.toString()
												},
												cache : false,

												success : function(
														html) {
													alert("Delete successfully");
													refresh();
												},
												error : function(
														html) {
													alert("Delete failly");
													refresh();
												}
											});
								} else {
									return;
								}
							} else {
								alert("Please select the record to delete");
								return;
							}
						});
					
					/**
					  上架按钮事件
					 */
					$("#tools") .delegate(
						'#shelveBtn',
						'click',
						function() {
							sheChk = $('input[name="gdId"]:checked');

							if (sheChk.length > 0) {
								if (confirm("你确定要将礼品上架?")) {

									var ids = "";
									var sheUrl = "shelve.do";
                                  var idArr=new Array();
                                 
                                  
                                  for (var i = 0; i < sheChk.length; i++) {
                                  	idArr.push(sheChk.get(i).value);
                             
								}
									
									$.ajax({
												url : sheUrl,
												type: "POST",
												data : {
													ids : idArr.toString()
												},
												cache : false,

												success : function(
														html) {
													alert("上架成功");
													refresh();
												},
												error : function(
														html) {
													alert("上架失败");
													refresh();
												}
											});
								} else {
									return;
								}
							} else {
								alert("请选择要上架的礼品");
								return;
							}
						});
					
					/**
					  下架按钮事件
					 */
					$("#tools")
					.delegate(
							'#unShelveBtn',
							'click',
							function() {
								unSheChk = $('input[name="gdId"]:checked');
								
								if (unSheChk.length > 0) {
									if (confirm("你确定要将礼品下架?")) {
										
										var ids = "";
										var unSheUrl = "unShelve.do";
										var idArr=new Array();
										
										
										for (var i = 0; i < unSheChk.length; i++) {
											idArr.push(unSheChk.get(i).value);
											
										}
										
										$.ajax({
											url : unSheUrl,
											type: "POST",
											data : {
												ids : idArr.toString()
											},
											cache : false,
											
											success : function(
													html) {
												alert("下架成功");
												refresh();
											},
											error : function(
													html) {
												alert("下架失败");
												refresh();
											}
										});
									} else {
										return;
									}
								} else {
									alert("请选择要下架的礼品");
									return;
								}
							});
					
					
					/**
					 * 添加
					 */
					$("#tools").delegate('#addGiftDetail', 'click', function() {
						var giId = $("#giftInfosId").val();
						var editUrl = "add.do";
						$.ajax({
							url : editUrl,
							dataType : 'json',
							data : {
								id : giId
							},
							cache : false,

							success : function(data) {

								$("#gdDetails input").each(function() {
									$(this).prop("value", null);
									$(this).prop("readonly", false);

								});
								$("#gdDetails textarea").each(function() {
									$(this).prop("value", null);

								});
								$(".readonlyss").each(function() {
									$(this).prop("readonly", true);
									
								});
								$("#color").each(function() {
									ue.setContent("");
									
								});
								
								//初始化表单的验证提示信息
								var i = 0;
								$(".jqtransform").Validform().resetForm();
								$(".Validform_checktip").each(function() {
									if(promtArry.length >= promtNum && promtArry != null){
										$(this).text(promtArry[i]);
										$(this).removeAttr("class");  
										$(this).attr("class","Validform_checktip");
										
										$(this).parent().prev().children().removeAttr("class");
										$(this).parent().prev().children().attr("class","inputxt Validform_error");
									}
									i++;
								});
								
//								$(".readonlyss").attr({ readonly: 'true' });
								loadGI(data);
								
							},
							error : function(html) {
								alert(html);
								alert("Load Data failly");
								refresh();
							}
						});
						
					
					});
					
					/**获取开始的所有验证信息*/
					$(".Validform_checktip").each(function() {
						promtNum++;
						var prompt = $(this).text();
						promtArry.push(prompt);
					});
				
				});


/**
 * 修改页面上的默认值
 */
function loadGI(giftDetail) {
	$("#giftInforId").val(giftDetail.giftInfor.id);
	$("#gdId").val(giftDetail.id);
	$("#giftCode").val(giftDetail.giftCode);
	$("#giftName").val(giftDetail.giftName);
	$("#width").val(giftDetail.width);
	$("#height").val(giftDetail.height);
	$("#longiness").val(giftDetail.longiness);
	$("#detailNum").val(giftDetail.detailNum);
	
	$("#color").val(giftDetail.color);
	$("#gdCode").val(giftDetail.gdCode);
	$("#gdName").val(giftDetail.gdName);
	$("#discount").val(giftDetail.discount);
	$("#pic").val(giftDetail.pic);
	$("#enterPrice").val(giftDetail.enterPrice);
	$("#salePrice").val(giftDetail.salePrice);
	$("#groupPrice").val(giftDetail.groupPrice);
	$("#mailPrice").val(giftDetail.mailPrice);
	$("#totals").val(giftDetail.totals);
	$("#isGroup").val(giftDetail.isGroup);
	$("#isPackage").val(giftDetail.isPackage);
	$("#isPromote").val(giftDetail.isPromote);
	$("#isMail").val(giftDetail.isMail);
	if(giftDetail.id==null || giftDetail.id=="" ){
		$("#ispush").val(giftDetail.giftInfor.ispush);
	}else{
		$("#ispush").val(giftDetail.ispush);
	}
	$("#giId").val(giftDetail.giftInfor.id);
	$("#giftInfor").val(giftDetail.giftInfor.id);
	
	//富文本编辑器
//	ue.setContent(giftDetail.color_str);
	
	$("#gdDetails").modal('show');
}

function vaildate(form) {
	return true;
}
/**
 * 刷新页面
 * */
function refresh() {
	window.location.reload();
}
